﻿using LPP.Service.MapServices;
using Microsoft.AspNetCore.Mvc;

namespace LPP.API.Controllers.MapController
{
    [ApiController]
    [Route("api/[controller]")]
    public class LocationController : ControllerBase
    {
        private readonly ILocationService _locationService;
        private readonly ILogger<LocationController> _logger;

        public LocationController(
            ILocationService locationService,
            ILogger<LocationController> logger)
        {
            _locationService = locationService;
            _logger = logger;
        }

        [HttpGet("address")]
        public async Task<IActionResult> GetAddress(
            [FromQuery] double longitude,
            [FromQuery] double latitude)
        {
            try
            {
                var result = await _locationService.GetLocationInfoAsync(longitude, latitude);
                if (!result.Success)
                {
                    return BadRequest(new { message = result.Message });
                }

                return Ok(result.Data);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取地址信息失败");
                return StatusCode(500, new { message = "获取地址信息失败" });
            }
        }
    }
}
